package org.example.job;

import com.alibaba.fastjson.JSON;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.example.ann.DynamicExecutor;
import org.example.ann.DynamicJob;
import org.example.entity.dto.XxlJobTask;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
 * @author zengxiaoyu
 * @date 2025/9/12 12:50
 */
@Component
@Slf4j
public class JobHandler {


    @Bean
    public String xxlJobExecutorParam() {
        XxlJobTask task = new XxlJobTask();
        task.setName("xxlJobExecutorParam");
        task.setValue("1");
        return JSON.toJSONString(task);
    }

    public static final String EXECUTOR_NAME = "cqt-executor-H";
    public static final String EXECUTOR_TITLE = "执行器H";
    public static final int EXECUTOR_PORT = 9020;


    public static final String EXECUTOR_NAME_BACKUP = "cqt-executor-I";
    public static final String EXECUTOR_TITLE_BACKUP = "执行器I";
    public static final int EXECUTOR_PORT_BACKUP = 9021;


    /// 格式: 服务名-业务类型-操作
    @XxlJob("xxlJobExecutor")   // ExecutorRegistryThread 会将xxlJobExecutor 告诉 xxlJob服务
    @DynamicJob(
            executor = @DynamicExecutor(name = EXECUTOR_NAME, title = EXECUTOR_TITLE, port = EXECUTOR_PORT),
            jobDesc = "cqt-测试-A",
            executorParam = "${xxlJobExecutorParam}",
            cron = "0/5 * * * * ?"  // 每5秒执行一次  // 每5秒执行一次
    )
    public void xxlJobExecutor() {
        String param = XxlJobHelper.getJobParam();
        XxlJobHelper.log("XXL-JOB, Hello World. Param: " + param);
        // 2. 执行业务逻辑
        try {
            // 这里写你的业务代码
            // 例如：调用你的业务方法
            log.info("执行动态任务A: " + param);

            // 3. 标记任务成功
            XxlJobHelper.handleSuccess();
        } catch (Exception e) {
            // 4. 标记任务失败
            XxlJobHelper.log("任务执行失败A: " + e.getMessage());
            XxlJobHelper.handleFail();
        }
    }


    @XxlJob("xxlJobExecutorB")
    @DynamicJob(
            executor = @DynamicExecutor(name = EXECUTOR_NAME, title = EXECUTOR_TITLE, port = EXECUTOR_PORT),
            jobDesc = "cqt-测试-B",
            executorParam = "${xxlJobExecutorParam}",
            cron = "0/20 * * * * ?"  // 每20秒执行一次  // 每5秒执行一次
    )
    public void xxlJobExecutorB() {
        String param = XxlJobHelper.getJobParam();
        XxlJobHelper.log("XXL-JOB, Hello World. Param: " + param);
        // 2. 执行业务逻辑
        try {
            // 这里写你的业务代码
            // 例如：调用你的业务方法
            log.info("执行动态任务B: " + param);

            // 3. 标记任务成功
            XxlJobHelper.handleSuccess();
        } catch (Exception e) {
            // 4. 标记任务失败
            XxlJobHelper.log("任务执行失败B: " + e.getMessage());
            XxlJobHelper.handleFail();
        }
    }


    @XxlJob("xxlJobExecutorC")
    @DynamicJob(
            executor = @DynamicExecutor(name = EXECUTOR_NAME_BACKUP, title = EXECUTOR_TITLE_BACKUP, port = EXECUTOR_PORT_BACKUP),
            jobDesc = "cqt-测试-C",
            executorParam = "${xxlJobExecutorParam}",
            cron = "0/20 * * * * ?"  // 每20秒执行一次  // 每5秒执行一次
    )
    public void xxlJobExecutorC() {
        String param = XxlJobHelper.getJobParam();
        XxlJobHelper.log("XXL-JOB, Hello World. Param: " + param);
        // 2. 执行业务逻辑
        try {
            // 这里写你的业务代码
            // 例如：调用你的业务方法
            log.info("执行动态任务C: " + param);

            // 3. 标记任务成功
            XxlJobHelper.handleSuccess();
        } catch (Exception e) {
            // 4. 标记任务失败
            XxlJobHelper.log("任务执行失败B: " + e.getMessage());
            XxlJobHelper.handleFail();
        }
    }
}
